package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.dto.GoodsParamsDto;
import com.castle.fortress.admin.goods.entity.GoodsParamsEntity;
import com.castle.fortress.admin.goods.mapper.GoodsParamsMapper;
import com.castle.fortress.admin.goods.service.GoodsParamsService;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品参数表 服务实现类
 *
 * @author majunjie
 * @since 2021-11-25
 */
@Service
public class GoodsParamsServiceImpl extends ServiceImpl<GoodsParamsMapper, GoodsParamsEntity> implements GoodsParamsService {
    /**
    * 获取查询条件
    * @param goodsParamsDto
    * @return
    */
    public QueryWrapper<GoodsParamsEntity> getWrapper(GoodsParamsDto goodsParamsDto){
        QueryWrapper<GoodsParamsEntity> wrapper= new QueryWrapper();
        if(goodsParamsDto != null){
            GoodsParamsEntity goodsParamsEntity = ConvertUtil.transformObj(goodsParamsDto,GoodsParamsEntity.class);
            wrapper.like(goodsParamsEntity.getId() != null,"id",goodsParamsEntity.getId());
            wrapper.like(goodsParamsEntity.getGoodsId() != null,"goods_id",goodsParamsEntity.getGoodsId());
            wrapper.like(StrUtil.isNotEmpty(goodsParamsEntity.getName()),"name",goodsParamsEntity.getName());
            wrapper.like(StrUtil.isNotEmpty(goodsParamsEntity.getValue()),"value",goodsParamsEntity.getValue());
            wrapper.like(goodsParamsEntity.getSort() != null,"sort",goodsParamsEntity.getSort());
            wrapper.like(goodsParamsEntity.getCreateUser() != null,"create_user",goodsParamsEntity.getCreateUser());
            wrapper.like(goodsParamsEntity.getCreateTime() != null,"create_time",goodsParamsEntity.getCreateTime());
            wrapper.like(goodsParamsEntity.getUpdateUser() != null,"update_user",goodsParamsEntity.getUpdateUser());
            wrapper.like(goodsParamsEntity.getUpdateTime() != null,"update_time",goodsParamsEntity.getUpdateTime());
            wrapper.like(goodsParamsEntity.getStatus() != null,"status",goodsParamsEntity.getStatus());
            wrapper.like(goodsParamsEntity.getIsDeleted() != null,"is_deleted",goodsParamsEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsParamsDto> pageGoodsParams(Page<GoodsParamsDto> page, GoodsParamsDto goodsParamsDto) {
        QueryWrapper<GoodsParamsEntity> wrapper = getWrapper(goodsParamsDto);
        Page<GoodsParamsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsParamsEntity> goodsParamsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsParamsDto> pageDto = new Page(goodsParamsPage.getCurrent(), goodsParamsPage.getSize(),goodsParamsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsParamsPage.getRecords(),GoodsParamsDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsParamsDto> pageGoodsParamsExtends(Page<GoodsParamsDto> page, GoodsParamsDto goodsParamsDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsParamsEntity goodsParamsEntity = ConvertUtil.transformObj(goodsParamsDto,GoodsParamsEntity.class);
        List<GoodsParamsEntity> goodsParamsList=baseMapper.extendsList(pageMap,goodsParamsEntity);
        Long total = baseMapper.extendsCount(goodsParamsEntity);
        Page<GoodsParamsDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsParamsList,GoodsParamsDto.class));
        return pageDto;
    }
    @Override
    public GoodsParamsDto getByIdExtends(Long id){
        GoodsParamsEntity  goodsParamsEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsParamsEntity,GoodsParamsDto.class);
    }

    @Override
    public List<GoodsParamsDto> listGoodsParams(GoodsParamsDto goodsParamsDto){
        QueryWrapper<GoodsParamsEntity> wrapper = getWrapper(goodsParamsDto);
        List<GoodsParamsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsParamsDto.class);
    }

    @Override
    public void deleteByGoodsId(Long goodsId) {
        if(goodsId == null){
            return;
        }
        baseMapper.deleteByGroupId(goodsId);
    }

    @Override
    public List<GoodsParamsDto> listByGoodsId(Long goodsId) {
        if(goodsId == null){
            return new ArrayList<>();
        }
        QueryWrapper<GoodsParamsEntity> wrapper= new QueryWrapper();
        wrapper.eq("goods_id" , goodsId);
        return ConvertUtil.transformObjList(baseMapper.selectList(wrapper) ,GoodsParamsDto.class );
    }
}

